package org.linlinjava.litemall.web.controller;

import javax.validation.constraints.NotBlank;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.web.annotation.LogAnno;
import org.linlinjava.litemall.web.annotation.LoginShop;
import org.linlinjava.litemall.web.annotation.LoginUser;
import org.linlinjava.litemall.web.service.WebGiftCardService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.math.BigDecimal;
import static org.linlinjava.litemall.web.util.WebResponseCode.INVALID_CARDNO;
/**
 * 礼物卡操作接口
 *
 * @author yliyun
 * @description：礼物卡
 * @date 2022/09/14
 */
@Api(tags="POS礼物卡操作API")
@RestController
@RequestMapping("/web/giftCard")
@RequiredArgsConstructor
public class WebGiftCardController {

    private final WebGiftCardService giftCardService;

    @ApiOperation(value="POS礼物卡-「查询卡号」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNo",value = "卡号"),
    })
    @GetMapping("/query")
    @LogAnno
    public Response query(@ApiParam(name="cardNo",value="卡号") @NotBlank String cardNo){
        if (cardNo == null || cardNo == "") {
            return ResponseUtil.fail(INVALID_CARDNO,"错误的卡号");
        }
        return giftCardService.query(cardNo);
    }


    @ApiOperation("POS充值查询-「礼物卡或者会员信息」")
    @ApiImplicitParam(name = "code",value = "码")
    @GetMapping("/findGiftOrUser")
    @LogAnno
    public Response findGiftOrUser(@RequestParam("code")String code){
        if (code == null || code == "") {
            return ResponseUtil.fail(INVALID_CARDNO,"错误的code,是空的");
        }
        return giftCardService.findGiftOrUser(code);
    }



    @ApiOperation(value = "POS现金充值-「充值礼物卡｜账户余额 后打印小票的信息」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "充值类型(1 礼物卡｜2 账户余额 ｜3 clover充值余额 |4 clover充值礼物卡|5 余额充值礼物卡|6 礼物卡充值余额|7 礼物卡充值礼物卡)"),
            @ApiImplicitParam(name = "cardNo",value = "充值的礼物卡号"),
            @ApiImplicitParam(name = "deviceNo",value = "设备编号"),
            @ApiImplicitParam(name = "membershipNumber",value = "会员号"),
            @ApiImplicitParam(name = "amount",value = "充值金额"),
            @ApiImplicitParam(name = "cash",value = "收取金额"),
            @ApiImplicitParam(name = "cloverNo",value = "clover卡号"),
    })
    @GetMapping("rechargeOfReceipt")
    @LogAnno
    public ResponseUtil.Response rechargeOfReceipt(@ApiIgnore @LoginShop Integer shopId,
                                                   @ApiIgnore @LoginUser Integer userId,
                                                   @RequestParam("type")Integer type,
                                                   @RequestParam("cardNo")String cardNo,
                                                   @RequestParam("deviceNo")String deviceNo,
                                                   @RequestParam(value = "membershipNumber",required = false)String membershipNumber,
                                                   @RequestParam("amount")BigDecimal amount,
                                                   @RequestParam("cash")BigDecimal cash,
                                                   @RequestParam(value = "cloverNo",required = false) String cloverNo) throws Exception {
        return giftCardService.rechargeOfReceipt(shopId,userId,type,cardNo,deviceNo,membershipNumber,amount,cash,cloverNo);
    }


    @ApiOperation(value="POS充值礼物卡-「现金」")
    @PutMapping("/recharge")
    @LogAnno
    public Response recharge(@ApiParam(name="cardNo",value="卡号",required=true) @NotBlank String cardNo,
                            @ApiParam(name="amount",value="金额",required=true) @NotBlank BigDecimal amount,
                            @ApiParam(name = "receiveAmount",value = "收取金额",required = true)BigDecimal receiveAmount,
                             @ApiParam(name = "deviceNo",value = "设备编号",required = true)String deviceNo,
                            @LoginUser Integer userId, @LoginShop Integer shopId){
        return giftCardService.recharge(cardNo,amount,userId,shopId,receiveAmount,deviceNo);
    }


    @ApiOperation(value = "POS充值礼物卡-「用户余额」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNo",value = "礼物卡号",required = true),
            @ApiImplicitParam(name = "amount",value = "充值金额",required = true),
            @ApiImplicitParam(name = "membershipNumber",value = "用户会员号",required = true)
    })
    @PutMapping("rechargeByAmount")
    @LogAnno
    public Response rechargeByAmount(@RequestParam("cardNo")@NotBlank String cardNo,
                                     @RequestParam("amount")@NotBlank BigDecimal amount,
                                     @RequestParam("membershipNumber") String membershipNumber,
                                     @ApiIgnore @LoginShop Integer shopId){
        return giftCardService.rechargeByAmount(cardNo,amount,membershipNumber,shopId);
    }

    @ApiOperation(value = "POS充值礼物卡-「用户礼物卡」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rechargeCardNo",value = "充值礼物卡号",required = true),
            @ApiImplicitParam(name = "payCardNo",value = "支付礼物卡号",required = true),
            @ApiImplicitParam(name = "amount",value = "充值金额",required = true)
    })
    @PutMapping("rechargeByGiftCard")
    @LogAnno
    public Response rechargeByGiftCard(@RequestParam("rechargeCardNo")@NotBlank String rechargeCardNo,
                                       @RequestParam("payCardNo")@NotBlank String payCardNo,
                                       @RequestParam("amount")@NotBlank BigDecimal amount,
                                       @ApiIgnore @LoginShop Integer shopId){
        return giftCardService.rechargeByGiftCard(rechargeCardNo,payCardNo,amount,shopId);
    }

    @ApiOperation(value = "POS充值礼物卡-「Clover｜保存交易记录」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "externalId",value = "externalId"),
            @ApiImplicitParam(name = "paymentId",value = "paymentId"),
            @ApiImplicitParam(name = "amount",value = "充值金额"),
            @ApiImplicitParam(name = "cardNo",value = "信用卡卡号"),
            @ApiImplicitParam(name = "cardType",value = "信用卡类型"),
            @ApiImplicitParam(name = "giftCardNo",value = "礼物卡卡号"),
            @ApiImplicitParam(name = "orderId",value = "clover支付订单")
    })
    @PutMapping("rechargeSaveByClover")
    @LogAnno
    public Response rechargeSaveByClover(@RequestParam String externalId,
                                         @RequestParam String paymentId,
                                         @RequestParam("amount")@NotBlank BigDecimal amount,
                                         String cardNo,String cardType,
                                         @RequestParam String giftCardNo,
                                         @RequestParam String orderId,
                                         @ApiIgnore @LoginShop Integer shopId){
        return giftCardService.rechargeSaveByClover(externalId,paymentId,amount,cardNo,cardType,giftCardNo, orderId,shopId);
    }


}
